<template>
  <div class="reg-container">
    <el-card shadow="hover">
      <div class="reg-title"></div>
      <el-form ref="form" :model="userInfo" :rules="rules">
        <el-form-item prop="username">
          <el-input placeholder="请输入用户名" prefix-icon="el-icon-user" v-model="userInfo.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
            v-model="userInfo.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            placeholder="请再次确认密码"
            prefix-icon="el-icon-lock"
            show-password
            v-model="userInfo.repassword"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="reg">注册</el-button>
        </el-form-item>
        <el-link :underline="false" @click="$router.push('/login')">已有账号去登录</el-link>
      </el-form></el-card
    >
  </div>
</template>
<script>
import { regAPI } from '@/api/user.js'
export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value !== this.userInfo.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      userInfo: {
        username: '',
        password: '',
        repassword: ''
      },
      rules: {
        username: [
          // required表示必填，message验证不通过时的提示，trigger，表示什么时候触发验证
          { required: true, message: '请输入用户名', trigger: 'blur' },
          // { min: 1, max: 10, message: '用户名长度1~10位', trigger: 'blur' }
          {
            pattern: /^[a-zA-Z0-9]{1,10}$/,
            message: '用户名必须是长度1-10位的字母数字',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '密码长度6~15位', trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: '请输入确认密码', trigger: 'blur' },
          { min: 6, max: 15, message: '确认密码长度6~15位', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reg() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          const { data: res } = await regAPI(this.userInfo)
          console.log(res)
          if (res.code === 0) {
            this.$message({
              message: res.message,
              type: 'success'
            })
            this.$refs.form.resetFields()
            this.$router.push('/login')
          } else {
            this.$message({
              message: res.message,
              type: 'warning'
            })
          }
        }
      })
    }
  }
}
</script>
<style lang="less">
.reg-container {
  height: 100%;
  background: url(../assets/images/login_bg.jpg) no-repeat;
  background-size: cover;
}
.el-card {
  // height: 355px; // 高度随便加一个，最后完成后，去掉，让高度自动撑开
  width: 400px;
  position: absolute;
  left: 50%; // 针对父级元素的50%
  top: 50%;
  transform: translate(-50%, -50%); // 向回平移自身的50%
}
.reg-title {
  height: 60px;
  background: url(../assets/images/login_title.png) 50% no-repeat;
}
.el-button {
  width: 100%;
}
</style>
